package com.haoxiaozhang.hxz_app.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.haoxiaozhang.hxz_app.R;
import com.haoxiaozhang.hxz_app.adapter.EmployeesAdapter;
import com.haoxiaozhang.hxz_app.bean.Employee;
import com.haoxiaozhang.hxz_app.business.EmployeesBusiness;
import com.haoxiaozhang.hxz_app.net.CommonCallback;
import com.haoxiaozhang.hxz_app.utils.T;
import com.haoxiaozhang.hxz_app.viewLib.refresh.SwipeRefresh;
import com.haoxiaozhang.hxz_app.viewLib.refresh.SwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class EmployeesListActivity extends BaseActivity {

    private EmployeesBusiness employeesBusiness = new EmployeesBusiness();
    private RecyclerView recyclerView;
    private Button button_tianjiayuangong;
    private EmployeesAdapter employeesAdapter;
    private List<Employee> listData = new ArrayList<>();
    private SwipeRefreshLayout swipeRefreshLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_employees_list);
        setBackToolBar("员工列表");

        initView();
        initEvent();

    }

    private void initEvent() {

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefresh.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });
        swipeRefreshLayout.setOnPullUpRefreshListener(new SwipeRefreshLayout.OnPullUpRefreshListener() {
            @Override
            public void onPullUpRefresh() {

            }
        });

        //添加员工事件
        button_tianjiayuangong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(EmployeesListActivity.this,AddEmployeesActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        startLoadingProgress();
        initData();
    }

    private void initView() {

        recyclerView = findViewById(R.id.id_recyclerview);
        employeesAdapter = new EmployeesAdapter(this,listData);
        swipeRefreshLayout = findViewById(R.id.id_refresh_layout);
        button_tianjiayuangong = findViewById(R.id.button_tianjiayuangong);

        //设置开关(上拉加载和下拉刷新）
        swipeRefreshLayout.setMode(SwipeRefresh.Mode.DISABLED); //暂不开启
        swipeRefreshLayout.setColorSchemeColors(Color.RED,Color.BLACK,Color.BLUE,Color.GRAY);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(employeesAdapter);


    }


    private void initData() {


        employeesBusiness.listEmployees(0, new CommonCallback<List<Employee>>() {
            @Override
            public void onError(Exception e) {
                stopLoadingProgress();
                T.showToast(e.getMessage());
            }

            @Override
            public void onSuccess(List<Employee> response) {
                stopLoadingProgress();
                listData.clear();
                listData.addAll(response);
                employeesAdapter.notifyDataSetChanged();

            }
        });

    }
}
